package com.jzy.message.activities.base;

import android.app.Activity;
import android.app.Application;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.PowerManager;
import android.util.Log;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import org.jivesoftware.smack.XMPPConnection;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/13.
 */
public class BaseApplication extends Application {
    public static XMPPConnection xmppConnection;
    private static BaseApplication application;
    /**/
    private final static String TAG = BaseApplication.class.getCanonicalName();
    private static BaseApplication sInstance;
    private static PackageManager sPackageManager;
    private static String sPackageName;
    private static String sDeviceURN;
    private static String sDeviceIMEI;
    private static int sSdkVersion;
    private static int sVersionCode;
    private static AudioManager sAudioManager;
    private static SensorManager sSensorManager;
    private static KeyguardManager sKeyguardManager;
    private static ConnectivityManager sConnectivityManager;
    private static PowerManager sPowerManager;
    private static PowerManager.WakeLock sPowerManagerLock;
    private static int sGlEsVersion;
    static final String sBuildModel = Build.MODEL.toLowerCase();
    static final String[] sSLEs2FriendlyBuildModels =
            {
                    "galaxy nexus", /* 4.1 */
                    "gt-i9100", /* Galaxy SII, 4.0.4 */
    	/*"gt-s5360",*//* 2.3.6 :robotic*/
                    "gt-s5570i", /* 2.3.0 */
                    "xt890", /* Motorola Razer i 4.0.4 */
                    "lg-p970" /* 2.3.4 */
            };
    static final String[] sSLEs2UnFriendlyBuildModels =
            {
                    "gt-s5360",/* 2.3.6 :robotic*/
            };
    static final String[] sSetModeFriendlyBuildModels =
            {
                    "gt-s5570i" /* 2.3.6 */
            };
    // This function is called by the package manager, you must never explicitly invoke it.
    // Do not forget to add/change the <application /> section in your manifest as done in IMSDroid or all other test apps
    public BaseApplication() {
        sInstance = this;
    }

    public static BaseApplication getInstance(){
        return sInstance;
    }

    /**
     * Retrieve application's context
     * @return Android context
     */
    public static Context getContext() {
        return getInstance();
    }
    /**/
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        application = this;
        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
        // xUtils的初始化
        //x.Ext.init(this);
        //x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
        //initImageLoader(getApplicationContext());

        /**/
        sPackageManager = sInstance.getPackageManager();
        sPackageName = sInstance.getPackageName();

        Log.d(TAG,"Build.MODEL="+sBuildModel);
        Log.d(TAG,"Build.VERSION.SDK="+Build.VERSION.SDK);
        /**/
    }
    public static void initImageLoader(Context context) {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 2)// 设置线程的优先级
                .denyCacheImageMultipleSizesInMemory()// 当同一个Uri获取不同大小的图片，缓存到内存时，只缓存一个。默认会缓存多个不同的大小的相同图片
                .discCacheFileNameGenerator(new Md5FileNameGenerator())// 设置缓存文件的名字
                .discCacheFileCount(60)// 缓存文件的最大个数
                .tasksProcessingOrder(QueueProcessingType.LIFO)// 设置图片下载和显示的工作队列排序
                .build();
        ImageLoader.getInstance().init(config);
    }
    // 运用list来保存们每一个activity是关键
    private List<Activity> mList = new LinkedList<Activity>();
    private static BaseApplication instance;

    /*// 构造方法
    // 实例化一次
    public synchronized static BaseApplication getInstance() {
        if (null == instance) {
            instance = new BaseApplication();
        }
        return instance;
    }
    public static Context getContext() {
        return application;
    }*/
    // add Activity
    public void addActivity(Activity activity) {
        mList.add(activity);
    }

    // 关闭每一个list内的activity
    public void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }


}
